<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="24" :xs="24">
        <el-form
          v-show="showSearch"
          ref="queryForm"
          :model="queryParams"
          :inline="true"
          label-width="100px"
        >
          <el-form-item label="医生名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入医生名称"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />

          </el-form-item>

          <el-form-item label="校医室" prop="status">
            <el-select
              v-model="queryParams.departmentId"
              placeholder="校医室名称"
              clearable
              size="small"
              style="width: 240px"
            >
              <el-option v-for="(item,index) in departmentList" :key="item.id" :label="item.departmentName" :value="item.id"/>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
            >搜索
            </el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
            >新增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              @click="handleDelete"
            >删除
            </el-button>
          </el-col>
          <el-table :data="doctorList" style="width: 100%" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55"/>
            <el-table-column prop="doctorName" label="医生名称" align="center"/>
            <el-table-column prop="userName" label="医生账号" align="center"/>
            <el-table-column  v-if="false" prop="id" label="id" align="center" hidden/>
            <el-table-column prop="introduction" label="简介" align="center"/>
            <el-table-column prop="departmentName" label="校医室" align="center"/>
            <el-table-column prop="departmentId" label="校医室编号" align="center"/>
            <el-table-column prop="sex" label="性别" align="center">
              <template slot-scope="scope">
                {{ scope.row.sex === '0' ? '男' : '女' }}
              </template>
            </el-table-column>
            <el-table-column prop="email" label="邮箱信息" align="center">
            </el-table-column>
            <el-table-column prop="status" label="状态" align="center">
              <template slot-scope="scope">
                <el-switch
                  :value="scope.row.status"
                  active-value="0"
                  inactive-value="1"
                  @change="handleStatusChange(scope.row)"
                />
              </template>
            </el-table-column>

            <el-table-column prop="createTime" label="创建时间" align="center"/>
            <el-table-column
              label="操作"
              align="center"
              width="160"
              class-name="small-padding fixed-width"
            >
              <template v-if="scope.row.id !== 1" slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                >修改</el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                >删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-row>
      </el-col>
    </el-row>

    <el-pagination
      :page-size.sync="queryParams.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      :page-sizes="[10, 20, 30, 40]"
      :current-page.sync="queryParams.pageNum"
      @current-change="getList"
      @size-change="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">


            <el-form-item label="医生昵称" prop="doctorName">
              <el-input
                v-model="form.doctorName"
                placeholder="请输入医生昵称"
                maxlength="30"
              />
            </el-form-item>

          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
          <el-form-item  v-if="updateFlag" label="账号名称" prop="userName">
            <el-input
              v-model="form.userName"
              placeholder="请输入医生账号名称"
              maxlength="30"
            />
          </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否修改密码" v-show="!updateFlag">
              <el-switch
                v-model="updateFlagButton"
                active-value= "0"
                inactive-value="1"
                @change="updateFlagButton == 0?1:0"
              />
            </el-form-item>

            <el-form-item v-if="updateFlagButton == 0 || updateFlag" label="账号密码" prop="password">
              <el-input
                v-model="form.password"
                placeholder="请输入密码"
                maxlength="30"
              />
            </el-form-item>
          </el-col>
          </el-row>

            <el-form-item label="邮箱" prop="email">
              <el-input
                v-model="form.email"
                placeholder="请输入邮箱号"
                maxlength="11"
              />
            </el-form-item>

        <el-row>
          <el-col :span="12">
            <el-form-item label="校医室" prop="departmentId">
              <el-select
                v-model="form.departmentId"
                placeholder="校医室名称"
                clearable
                size="small"
                style="width: 180px"
              >
                <el-option v-for="(item,index) in departmentList" :key="item.id" :label="item.departmentName" :value="item.id"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别">
              <el-select  style="width: 180px" v-model="form.sex" placeholder="请选择">
                <el-option :key="'0'" label="男" :value="'0'" />
                <el-option :key="'1'" label="女" :value="'1'" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
            <el-form-item label="医生简介" prop="introduction">
              <el-input
                v-model="form.introduction"
                placeholder="请输入医生简介"
                type="textarea"
              />
            </el-form-item>




      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {allDepartmentList} from "@/api/department/department";
import {allDoctorList, addDoctor, updateDoctor, deleteDoctor} from "@/api/info/doctor";
import {validUser} from "@/api/user";
import {changeUserStatus} from "@/api/info/user";

export default {
  name: "index",
  data() {
    const validateUsername = (rule, value, callback) => {
      if(value =="" || value == undefined){
        callback(new Error('请输入用户名'))
        return
      }
      validUser(value, "2").then((response) => {
        if(response.code === -101) {
          callback()
        }else {
          callback(new Error('该账号名称已存在，请检查'))
        }
      }).catch(() => {

      })
    }
    return {
      showSearch: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        departmentId: undefined
      },
      queryDepartmentParams:{
        pageNum: 1,
        pageSize: 999
      },
      departmentList: null,
      doctorList: null,
      total: null,
      updateFlag: true,
      updateFlagButton: 0,
      open: false,
      ids: [],
      // 表单参数
      form: {},
      // 遮罩层
      loading: true,
      // 表单校验
      title: '',
      rules: {
        userName: [
          {required: true, trigger: 'blur' ,validator: validateUsername }
        ],
        doctorName: [
          {required: true, message: '用户昵称不能为空', trigger: 'blur'},
        ],
        password: [
          { required: true, message: '用户密码不能为空', trigger: 'blur' },
          {
            min: 5,
            max: 20,
            message: '用户密码长度必须介于 5 和 20 之间',
            trigger: 'blur'
          }
        ]
      },
    }
  },
  methods: {
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    getList() {
      this.loading = true
      allDoctorList(this.queryParams).then((response) => {
        this.doctorList = response.list;
        this.total = response.total
        this.loading = false
      });
      allDepartmentList(this.queryDepartmentParams).then((response) => {
        this.departmentList = response.list
      });
    },
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal
        .confirm('是否删除当前选中数据项？')
        .then(function () {
          return deleteDoctor(ids)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {
        })
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    // 表单重置
    reset() {
      this.form = {
        userName: undefined,
        score: undefined,
        scoreDetail: undefined,
        type: undefined,
        userId: undefined,
        id:undefined
      }
      this.resetForm('form')
    },
    handleAdd() {
      this.reset()
      this.open = true
      this.updateFlag = true;
      this.title = '添加医生信息';
      this.updateFlagButton = 0
    },
    handleUpdate(row) {
      this.reset()

      this.form = row;
      this.open = true;
      this.title = '修改医生信息';
      this.updateFlag = false;
      this.updateFlagButton = 1;
    },
    cancel() {
      this.open = false
      this.reset();
      this.updateFlag = true;
      this.updateFlagButton = 0
    },  /** 提交按钮 */
    submitForm: function () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id !== undefined) {
            updateDoctor(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.updateFlag = true;
              this.updateFlagButton = 0
              this.getList();
            })
          }else {
            addDoctor(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    handleStatusChange(row) {
      const text = row.status === "0" ? '禁用' : '启用';
      this.$modal
        .confirm('确认将该用户改为'+text+'状态')
        .then(function() {
          return changeUserStatus(row.id, row.status === "0"?"1":"0").then((response) => {
            row.status = row.status === "0"?"1":"0"
          })
        })
        .then(() => {
          this.$modal.msgSuccess(text + '成功');
          this.getList();
        })
        .catch(function() {
          row.status = row.status === '0' ? '1' : '0'
        })
    },
  },
  created() {
    this.getList()
  }
}
</script>

<style scoped>

</style>
